﻿using AntdUI;
using Wen.Core.Extensions;
using WenAntdUI.Extensions;
using WenAntdUI.Helps;

namespace WenAntdUI.Settings
{
    public partial class SettingsForm : WenForm
    {
        public SettingsForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = this.AppendVersion();


            var sttgs = Settings.AppSettings.Settings.GroupBy(a => a.GroupName);

            foreach (var stts in sttgs)
            {
                foreach (var item in stts)
                {
                    var type = item.Type is null ? typeof(string) : Type.GetType(item.Type);

                    var name = string.IsNullOrWhiteSpace(item.DisplayName) ? item.Name : item.DisplayName;

                    if (type is null || type.Equals(typeof(string)))
                    {
                        var control = new Input()
                        {
                            Width = flowLayoutPanel1.Width - 30,
                        }
                        .DataBinding(item, (a, b) => a.Text == b.Value);

                        AddControl(name, control);
                    }
                    else if (type.IsEnum)
                    {
                        var es = type.GetEnumNames();
                        var control = new AntdUI.Select()
                        {
                            Text = item.Value
                        };
                        control.Items.AddRange(es);
                        control.SelectedIndexChanged += (s, e) =>
                        {
                            var value = control.SelectedValue?.ToString();
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                item.Value = Enum.Parse(type, value).ToString();
                            }
                        };
                        AddControl(name, control);
                    }
                    else if (type.Equals(typeof(Color)))
                    {
                        var control = new AntdUI.ColorPicker()
                        {
                            ShowText = true,
                            Value = string.IsNullOrWhiteSpace(item.Value) ? Color.Empty : ColorTranslator.FromHtml(item.Value)
                        };
                        control.ValueChanged += (s, e) => item.Value = ColorTranslator.ToHtml(control.Value);
                        AddControl(name, control);
                    }
                }
            }
        }

        private void AddControl(string title, Control control)
        {
            var group = new AntdUI.Panel()
            {
                Width = flowLayoutPanel1.Width - 20,
                Height = 40,
            };

            var lable = new AntdUI.Label()
            {
                Text = title,
                Width = 130,
                Height = 40,
                Dock = DockStyle.Left,
            };
            control.Dock = DockStyle.Fill;
            group.Controls.Add(control);
            group.Controls.Add(lable);

            this.flowLayoutPanel1.Controls.Add(group);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            AppSettings.FSql.Update<Setting>().SetSource(AppSettings.Settings).ExecuteEffects();
            this.ShowInfo("保存成功！部分功能需要重启才能生效！");
        }
    }
}
